/**
 * Created With IntelliJ IDEA
 * Description:leetcode：141. 环形链表
 * <a href="https://leetcode.cn/problems/linked-list-cycle/description/">...</a>
 * User: DELL
 * Data: 2023-01-08
 * Time: 10:02
 */

class ListNode {
    int val;
    ListNode next;
    public ListNode (int val) {
        this.val = val;
    }
}
public class Solution {
    public boolean hasCycle(ListNode head) {
        //判空处理
        if (head == null) {
            return false;
        }
        ListNode fast = head; //快指针每次走两个结点
        ListNode slow = head; //慢指针每次走一个结点
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }
}